package basic.security;

import java.security.*;
import java.util.Scanner;
import java.io.*;

public class DigestInputLine {
	public static void main(String args[]) throws Exception {
		if (args.length == 0) {
			args = new String[1];
			System.out.println("Please input one file name, I will work out this file's content's MD5 digest:");
			Scanner in = new Scanner(System.in);
			args[0] = in.nextLine();
		}
		String fileName = args[0];
		MessageDigest m = MessageDigest.getInstance("MD5");
		FileInputStream fin = new FileInputStream(fileName);
		DigestInputStream din = new DigestInputStream(fin, m);
		din.on(false);
		int b;
		while ((b = din.read()) != -1) {
			if (b == '$') {
				din.on(true);
			}
		}
		byte s[] = m.digest();
		String result = "";
		for (int i = 0; i < s.length; i++) {
			result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
		}
		System.out.println(result);

	}
}
